package cn.wanda.bi.sunshine.platform.server.aspect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.util.CollectionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import cn.wanda.bi.sunshine.platform.server.SunshineCode;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.UserLabelAuthAppidMapper;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.UserLabelReturnFieldMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.UserLabelReturnField;
import cn.wanda.bi.sunshine.platform.server.bean.model.UserLabelReturnFieldExample;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelBean;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelHitsDetail;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import cn.wanda.bi.sunshine.platform.server.service.UserLabelReturnFieldService;
import cn.wanda.bi.sunshine.platform.server.utils.MapUtil;
import cn.wanda.bi.sunshine.platform.server.utils.exception.SunshineException;

/**
 * Created by wangfupeng1 on 2017-5-6.
 */
@Aspect
@Component
@Order(3)
public class AuthLabelSingleAspect {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserLabelReturnFieldService userLabelReturnFieldService;
    
    @Around("@annotation(cn.wanda.bi.sunshine.platform.server.aspect.AuthLabelSingle)")
    public Object aroundLabel(ProceedingJoinPoint pjp) throws Throwable {
        Object ob = pjp.proceed();
        SimpleResponse simpleResponse = (SimpleResponse) ob;
        UserLabelBean userLabelBean= (UserLabelBean) simpleResponse.getData();
        if(userLabelBean.getHits().getHits().size()==0)
        	return SimpleResponse.success();
        UserLabelHitsDetail hit= userLabelBean.getHits().getHits().get(0);
        String appid = request.getHeader("appid");
    	UserLabelReturnFieldExample userLabelReturnFieldExample = new UserLabelReturnFieldExample();
    	userLabelReturnFieldExample.createCriteria().andAppidEqualTo(appid);
    	List<UserLabelReturnField> userLabelReturnFields= userLabelReturnFieldService.getUserLabelReturnFields(userLabelReturnFieldExample, appid);
    	if(CollectionUtils.isEmpty(userLabelReturnFields))
    		throw new SunshineException(SunshineCode.APPID_WRONG_OR_secret_WRONG);
		Map<String,Object> hitMap = (Map<String,Object>)MapUtil.objToMap(hit.get_source());
		Map<String,Object> hitResult = new HashMap<String,Object>();
		userLabelReturnFields.forEach(dimUserLabelAllReturnField->{
			String field = dimUserLabelAllReturnField.getReturnField();
			hitResult.put(field, hitMap.get(field));
		});
        return SimpleResponse.success(hitResult);
    }
}
